package edu.ccut.saturn.workflow.query;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;

public class SplitTaskListByIsTimeout extends DefaultComponent {
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		ArrayList<SaturnData> tasklist = (ArrayList<SaturnData>) parameterContext
				.getParameterValue(0);
		ArrayList<SaturnData> timeouttasklist = new ArrayList<SaturnData>();
		ArrayList<SaturnData> normaltasklist = new ArrayList<SaturnData>();
		if (tasklist != null) {
			for (SaturnData task : tasklist) {
				String is_timeout = task.get("workitem_is_timeout") + ""; //$NON-NLS-1$ //$NON-NLS-2$
				String final_time = (String)task.get("workitem_final_time"); //$NON-NLS-1$ //$NON-NLS-2$
			//	String start_time = task.get("start_time")+"";
				if ("Y".equals(is_timeout)) { //$NON-NLS-1$
					// 越期
					timeouttasklist.add(task);
				} else {
					// 非越期
					//比对时间
					 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
				//	Long start_timeL = format.parse(start_time).getTime();
					 Long start_timeL =format.parse( Operation.baseService().GetSystemTime()).getTime();
					Long final_timeL  = 0L;
					Long interval = null;
					String intervalforshow = null;//插入任务剩余时间 回显给用户
					if(final_time!=null&&!"".equals(final_time)){ //$NON-NLS-1$
						 final_timeL = format.parse(final_time).getTime();
						 interval = final_timeL - start_timeL;
					}
					if(interval!=null){
						
						int sec1 = 1000;
						int min1 = sec1*60;
						int hour1 = min1*60;
						int day1 = hour1*24;
						
						int day = 0;
						while(interval>=day1){
							day++;
							interval=interval-day1;
						}
						int hour = 0;
						while(interval>=hour1){
							hour++;
							interval=interval-hour1;
						}
						int min = 0;
						while(interval>=min1){
							min++;
							interval=interval-min1;
						}
						int sec = 0;
						while(interval>=sec1){
							sec++;
							interval=interval-sec1;
						}
						intervalforshow =day+Messages.getString("SplitTaskListByIsTimeout.0")+hour+Messages.getString("SplitTaskListByIsTimeout.3")+min+Messages.getString("SplitTaskListByIsTimeout.2")+sec+Messages.getString("SplitTaskListByIsTimeout.1"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					}
					//插入intervalforshow
					task.set("workitem_interval", intervalforshow); //$NON-NLS-1$
					normaltasklist.add(task);
				}
			}
		}
		parameterContext.setParameter(1, timeouttasklist);
		parameterContext.setParameter(2, normaltasklist);
	}
}